
import numpy as np
from scipy.io import loadmat

from LearnPy.MIClustering.MyBamic.bag_dist_matrix import bag_dist_matrix, min_euclid_dist
from LearnPy.MIClustering.MyBamic.bamic import bamic


def trans_to_test(musk_test,musk_train,cluster_num,bag_center_dist):
    #返回可以用于测试的instance集，也就是把需要测试的包转换为可以用于本算法测试的instance
    #此测试集应包含多个用于测试的instance
    test_bag_num = len(musk_test["data"])
    train_bag_num = len(bag_center_dist)-1

    #测试集，横坐标数，表示待测试的包的个数，竖坐标表示簇中心的个数
    #test_data[i][j]表示第i个测试包到第j个簇中心的距离
    test_data = np.empty((test_bag_num,cluster_num))
    cluster_num = len(bag_center_dist[0])#簇的个数等于bag_center_dist的横坐标数
#j->int(bag_center_dist[train_bag_num][j])
    for i in range(test_bag_num):
        for j in range(cluster_num):
            euclid_a_b = min_euclid_dist(musk_test['data'][i][0][:, :-1], musk_train['data'][int(bag_center_dist[train_bag_num][j])][0][:, :-1])
            euclid_b_a = min_euclid_dist(musk_train['data'][int(bag_center_dist[train_bag_num][j])][0][:, :-1], musk_test['data'][i][0][:, :-1])
            instance_num_a = len(musk_test['data'][i][0])
            instance_num_b = len(musk_train['data'][int(bag_center_dist[train_bag_num][j])][0])
            distance = (euclid_a_b + euclid_b_a) / (instance_num_a + instance_num_b)

            test_data[i, j] = distance

    #TODO 算一下包到各个簇中心的距离
    print(test_data)
    return test_data




if __name__=="__main__":

   path1 = './musk_debug.mat'
   musk_test = loadmat(path1)
   path = './musk1+.mat'
   musk_train = loadmat(path)
   # 分簇的个数
   cluster_num = 14
   # 邻居个数
   n_neighbors = 3
   # 包与包的距离矩阵
   dist_matrix = bag_dist_matrix(musk_train)
   # 包与簇中心的距离矩阵
   bag_center_dist = bamic(musk_train, cluster_num, dist_matrix)

   trans_to_test(musk_test,musk_train,cluster_num,bag_center_dist)